<?php
    require_once('includes/common.inc.php');
    require_once('includes/lib_mail.inc.php');

    define('TPL_MESSAGE_DELIVERY_FAILED',   'include/feedback/message_delivery_failed.tpl');
    define('TPL_DELIVERY_SUCCESSFUL',       'include/feedback/delivery_successful.tpl');
    define('TPL_INCORRECT_CAPTCHA',         'include/feedback/incorrect_captcha.tpl');

    $pageName = "Feedback";
    $page = Pages::CreatePage( $pageName );
    $page->SetVar( "pageName", $pageName );

    $form = new FeedbackForm(Form::CAPTCHA_PROTECTION);

    if ( Request::IsMethod(Request::POST) )
    {
        $form->Bind();
        if ( $form->IsValid() )
        {
            try
            {
                $values = $form->GetValues();

                SendFeedback($values['name'], $values['email'], $values['message']);
                $page->SetQuotedVar('NAME', $values['name']);
                $page->SetQuotedVar('EMAIL', $values['email']);
                $page->SetQuotedVar('MESSAGE', $values['message']);
                $page->SetBodyTemplate(TPL_DELIVERY_SUCCESSFUL);
                $page->Display();
                exit();
            }
            catch ( MailSendException $e )
            {
                $page->SetVar('ADD_BLOCK', true);
                $page->ParseTemplate('ERROR_MESSAGE', TPL_MESSAGE_DELIVERY_FAILED);
                $form->SetError(null, $page->GetTemplateEngine()->getGenerated('ERROR_MESSAGE'));
            }
        }
    }

    $page->SetVar('FORM', $form->Generate());

    $page->Display();
?>